package com.model.bean;

import com.entites.Employee;
import com.entites.EventComment;
import com.entites.EventLibrary;
import com.entites.EventRegistration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class EventCommentFacade extends AbstractFacade<EventComment> implements EventCommentFacadeLocal {

    @PersistenceContext(unitName = "BluePumpkinCompanyPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EventCommentFacade() {
        super(EventComment.class);
    }

    @Override
    public List<EventComment> getEventCommentListPaging(int begin_row, int end_row, int eventId) {
        try {

            List<Object[]> objArr;
            List<EventComment> list = new ArrayList<>();
            Query query = em.createNamedStoredProcedureQuery("EventComment.getEventCommentPaging");
            query.setParameter("beginRow", begin_row);
            query.setParameter("endRow", end_row);
            query.setParameter("eventId", eventId);
            objArr = query.getResultList();

            for (Object[] row : objArr) {
                EventLibrary evlbr = new EventLibrary();
                Employee epl = new Employee();
                int id = (int) row[0];
                evlbr.setEventId((Integer) row[1]);
                epl.setUsername((String) row[2]);
                String comment = (String) row[3];
                Date date = (Date) row[4];
                String avatar = (String) row[5];
                EventComment ev = new EventComment(id, evlbr, epl, comment, date,avatar);
                list.add(ev);
            }
            return list;

        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public int countCommentByEventId(int eventId) {
        Query query = em.createNamedStoredProcedureQuery("EventComment.getCountCommentByEventId");
        query.setParameter("eventId", eventId);
        List<Object[]> arr = query.getResultList();
        int count = arr.size();
        return count;
    }

    @Override
    public List<EventComment> getEventCommentByUser(int begin_row, int end_row, String username) {
        List<Object[]> obj;
        List<EventComment> list = new ArrayList<>();

        Query showAll = em.createNamedStoredProcedureQuery("EventComment.getEventCommentByUser");
        showAll.setParameter("beginRow", begin_row);
        showAll.setParameter("endRow", end_row);
        showAll.setParameter("Username", username);
        obj = showAll.getResultList();
        for (Object[] row : obj) {
            int id = (int) row[0];
            String comment = (String) row[2];
            Date timeComment = (Date) row[3];
            EventLibrary el = new EventLibrary();
            el.setEventId(id);
            EventComment ec = new EventComment(comment, timeComment, el);
            list.add(ec);
        }
        return list;
    }

    @Override
    public void postComment(EventComment eventComment) {
        Query query = em.createNamedStoredProcedureQuery("EventComment.PostComment");
        query.setParameter("eventId", eventComment.getEvId());
        query.setParameter("username", eventComment.getUsn());
        query.setParameter("comment", eventComment.getComment());
        query.executeUpdate();

    }

}
